package linkedlist.test24;

import listbyorder.utils.ListNode;

/**
 * 两两交换链表中的节点
 * @author code_yc
 * @version 1.0
 * @date 2020/7/14 13:47
 */
public class Solution1 {

    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode a = dummy;
        ListNode b, c, d;
        while (a.next != null && a.next.next != null) {
            b = a.next.next;
            c = a.next;
            d = b.next;
            a.next = b;
            c.next = d;
            b.next = c;
            a = c;
        }
        return dummy.next;
    }
}
